package com.party.admin.biz.merchantAnalyze;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.party.common.paging.Page;
import com.party.common.utils.DateUtils;
import com.party.core.model.YesNoStatus;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.member.MerchantAnalyze;
import com.party.core.model.member.MerchantAnalyzeQuery;
import com.party.core.service.member.*;
import com.party.core.service.order.IOrderFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import static com.party.common.utils.DateUtils.addMonth;

/**
 * 机构数据分析
 * Created by wei.li
 *
 * @date 2017/9/13 0013
 * @time 9:24
 */

@Service
public class MerchantAnalyzeBizService {


    @Autowired
    private IMemberActService memberActService;

    @Autowired
    private IOrderFormService orderFormService;


    @Autowired
    private IMemberGroupService memberGroupService;

    @Autowired
    private IMerchantAnalyzeService merchantAnalyzeService;

    @Autowired
    private IPartnerMemberService partnerMemberService;

    //时间参数键
    private final static String DAY = "day";

    //总数参数键
    private final static String COUNT = "count";


    /**
     * 数据初始化
     */
    public void init(){
        List<MemberGroup> list = memberGroupService.getAllPartnerNoOpenStatus();
        for (MemberGroup m : list){



            /*

             //报名
            List<HashMap<String, Object>> count = memberActService.countByDate(null, null, m.getId());
            for (HashMap c : count){
                String date = (String) c.get(DAY);
                Integer num = Integer.valueOf(c.get(COUNT).toString());
                MerchantAnalyze merchantAnalyze = merchantAnalyzeService.getSet(m.getId(), DateUtils.convertDate(date));
                merchantAnalyze.setApplyNum(num);
                merchantAnalyze.setName(m.getRealname());
                merchantAnalyze.setMobile(m.getMobile());
                merchantAnalyzeService.update(merchantAnalyze);
            }

            //会员
            List<HashMap<String, Object>> count = partnerMemberService.countByDate(null, null, m.getId(), false);
            for (HashMap c : count){
                String date = (String) c.get(DAY);
                Integer num = Integer.valueOf(c.get(COUNT).toString());
                MerchantAnalyze merchantAnalyze = merchantAnalyzeService.getSet(m.getId(), DateUtils.convertDate(date));
                merchantAnalyze.setMemberNum(num);
                merchantAnalyze.setName(m.getRealname());
                merchantAnalyze.setMobile(m.getMobile());
                merchantAnalyzeService.update(merchantAnalyze);
            }

            List<HashMap<String, Object>> realNameCount = partnerMemberService.countByDate(null, null, m.getId(), true);
            for (HashMap rc : realNameCount){
                String date = (String) rc.get(DAY);
                Integer num = Integer.valueOf(rc.get(COUNT).toString());
                MerchantAnalyze merchantAnalyze = merchantAnalyzeService.getSet(m.getId(), DateUtils.convertDate(date));
                merchantAnalyze.setRealMemberNum(num);
                merchantAnalyze.setName(m.getRealname());
                merchantAnalyze.setMobile(m.getMobile());
                merchantAnalyzeService.update(merchantAnalyze);
            }
             //订单
            List<HashMap<String, Object>> orderCount = orderFormService.countByDate(null, null, m.getId());
            for (HashMap c : orderCount){
                String date = (String) c.get(DAY);
                Integer num = Integer.valueOf(c.get(COUNT).toString());
                MerchantAnalyze merchantAnalyze = merchantAnalyzeService.getSet(m.getId(), DateUtils.convertDate(date));
                merchantAnalyze.setOrderNum(num);
                merchantAnalyze.setName(m.getRealname());
                merchantAnalyze.setMobile(m.getMobile());
                merchantAnalyzeService.update(merchantAnalyze);
            }

            //金额
            List<HashMap<String, Double>> paymentCount = orderFormService.paymentSumByDate(null, null, m.getId());
            for (HashMap c : paymentCount){
                String date = (String) c.get(DAY);
                Double payment = (Double)c.get("sum");
                MerchantAnalyze merchantAnalyze = merchantAnalyzeService.getSet(m.getId(), DateUtils.convertDate(date));
                merchantAnalyze.setMoney(payment);
                merchantAnalyze.setName(m.getRealname());
                merchantAnalyze.setMobile(m.getMobile());
                merchantAnalyzeService.update(merchantAnalyze);
            }*/

          /*  //报名
            List<HashMap<String, Object>> count = memberActService.countByDate(null, null, m.getId());
            for (HashMap c : count){
                String date = (String) c.get(DAY);
                Integer num = Integer.valueOf(c.get(COUNT).toString());
                MerchantAnalyze merchantAnalyze = merchantAnalyzeService.getSet(m.getId(), DateUtils.convertDate(date));
                merchantAnalyze.setApplyNum(num);
                merchantAnalyze.setName(m.getRealname());
                merchantAnalyze.setMobile(m.getMobile());
                merchantAnalyzeService.update(merchantAnalyze);
            }

            //订单
            List<HashMap<String, Object>> orderCount = orderFormService.countByDate(null, null, m.getId());
            for (HashMap c : orderCount){
                String date = (String) c.get(DAY);
                Integer num = Integer.valueOf(c.get(COUNT).toString());
                MerchantAnalyze merchantAnalyze = merchantAnalyzeService.getSet(m.getId(), DateUtils.convertDate(date));
                merchantAnalyze.setOrderNum(num);
                merchantAnalyze.setName(m.getRealname());
                merchantAnalyze.setMobile(m.getMobile());
                merchantAnalyzeService.update(merchantAnalyze);
            }

            //金额
            List<HashMap<String, Double>> paymentCount = orderFormService.paymentSumByDate(null, null, m.getId());
            for (HashMap c : paymentCount){
                String date = (String) c.get(DAY);
                Double payment = (Double)c.get("sum");
                MerchantAnalyze merchantAnalyze = merchantAnalyzeService.getSet(m.getId(), DateUtils.convertDate(date));
                merchantAnalyze.setMoney(payment);
                merchantAnalyze.setName(m.getRealname());
                merchantAnalyze.setMobile(m.getMobile());
                merchantAnalyzeService.update(merchantAnalyze);
            }*/

            /*//访问
            Integer viewNum = dataCountService.viewCount(null, null, m.getId());
            MerchantAnalyze merchantAnalyze = merchantAnalyzeService.getSet(m.getId(), DateUtils.today());
            merchantAnalyze.setViewNum(viewNum);
            merchantAnalyze.setName(m.getRealname());
            merchantAnalyze.setMobile(m.getMobile());
            merchantAnalyzeService.update(merchantAnalyze);*/
        }
    }


    /**
     * 今天的数据
     */
    public void todayData(){
        List<MemberGroup> list = memberGroupService.getAllPartnerNoOpenStatus();
        Date today = DateUtils.today();
        String todayStr = DateUtils.formatDate(today);
        for (MemberGroup m : list){
            Integer applyNum = memberActService.count(m.getId(), todayStr, todayStr, false);
            Integer orderNum = orderFormService.count(todayStr, todayStr, m.getId(), false);
            Double payment = orderFormService.paymentSum(todayStr, todayStr, m.getId(), false);
            Integer memberNum = partnerMemberService.count(m.getId(), todayStr, todayStr, false);
            Integer realNameNum = partnerMemberService.count(m.getId(), todayStr, todayStr, true);
            MerchantAnalyze merchantAnalyze = merchantAnalyzeService.getSet(m.getId(), today);
            MerchantAnalyzeQuery query = new MerchantAnalyzeQuery();
            query.setMemberId(m.getId());
            query.setCreateEnd(DateUtils.formatDate(DateUtils.addDay(today, -1)));
            merchantAnalyze.setApplyNum(applyNum);
            merchantAnalyze.setOrderNum(orderNum);
            merchantAnalyze.setMoney(payment);
            merchantAnalyze.setMemberNum(memberNum);
            merchantAnalyze.setRealMemberNum(realNameNum);
            merchantAnalyze.setName(m.getRealname());
            merchantAnalyze.setMobile(m.getMobile());
            merchantAnalyzeService.update(merchantAnalyze);
        }
    }


    /**
     * 机构数据分析
     * @param merchantAnalyzeQuery 查询参数
     * @param page 分页参数
     * @return 分析列表
     */
    public List<MerchantAnalyze> list(MerchantAnalyzeQuery merchantAnalyzeQuery, Page page){
        if (null != merchantAnalyzeQuery.getTimeType() && !merchantAnalyzeQuery.getTimeType().equals(0)){
            String now = DateUtils.todayDate();
            String endTime = DateUtils.todayDate();
            String startTime = null;
            if (merchantAnalyzeQuery.getTimeType().equals(1)){
                startTime = now;
            }
            else if (merchantAnalyzeQuery.getTimeType().equals(2)){
                startTime = DateUtils.formatDate(DateUtils.addDay(now, -1));
                endTime = startTime;
            }
            else if (merchantAnalyzeQuery.getTimeType().equals(3)){
                startTime = DateUtils.formatDate(DateUtils.getFirstDayOfWeek(0));
                endTime = DateUtils.formatDate(DateUtils.getLastDayOfWeek(0));
            }
            else if (merchantAnalyzeQuery.getTimeType().equals(4)){
                startTime = DateUtils.formatDate(DateUtils.getFirstDayOfMonth(0));
                endTime = DateUtils.formatDate(DateUtils.getLastDayOfMonth(0));
            }
            else if (merchantAnalyzeQuery.getTimeType().equals(5)){
                startTime = null;
                endTime = null;
            }
            merchantAnalyzeQuery.setCreateStart(startTime);
            merchantAnalyzeQuery.setCreateEnd(endTime);
        }
        return merchantAnalyzeService.sumList(merchantAnalyzeQuery, page);
    }


    /**
     * 统计详情
     * @param memberId 会员编号
     * @return 详情
     */
    public HashMap<String, MerchantAnalyze> detail(String memberId){
        HashMap<String, MerchantAnalyze> map = Maps.newHashMap();

        String now = DateUtils.todayDate();
        MerchantAnalyzeQuery merchantAnalyzeQuery = new MerchantAnalyzeQuery();
        merchantAnalyzeQuery.setMemberId(memberId);
        merchantAnalyzeQuery.setCreateStart(now);
        merchantAnalyzeQuery.setCreateEnd(now);

        //今日
        MerchantAnalyze today = merchantAnalyzeService.sum(merchantAnalyzeQuery);

        //昨日
        String day = DateUtils.formatDate(DateUtils.addDay(now, -1));
        merchantAnalyzeQuery.setCreateStart(day);
        merchantAnalyzeQuery.setCreateEnd(day);
        MerchantAnalyze yesterday = merchantAnalyzeService.sum(merchantAnalyzeQuery);

        //前日
        day = DateUtils.formatDate(DateUtils.addDay(now, -2));
        merchantAnalyzeQuery.setCreateStart(day);
        merchantAnalyzeQuery.setCreateEnd(day);
        MerchantAnalyze beforeYDay = merchantAnalyzeService.sum(merchantAnalyzeQuery);

        //本周
        merchantAnalyzeQuery.setCreateStart(DateUtils.formatDate(DateUtils.getFirstDayOfWeek(0)));
        merchantAnalyzeQuery.setCreateEnd(DateUtils.formatDate(DateUtils.getLastDayOfWeek(0)));
        MerchantAnalyze week = merchantAnalyzeService.sum(merchantAnalyzeQuery);

        //本月
        merchantAnalyzeQuery.setCreateStart(DateUtils.formatDate(DateUtils.getFirstDayOfMonth(0)));
        merchantAnalyzeQuery.setCreateEnd(DateUtils.formatDate(DateUtils.getLastDayOfMonth(0)));
        MerchantAnalyze month = merchantAnalyzeService.sum(merchantAnalyzeQuery);

        //历史
        merchantAnalyzeQuery.setCreateStart(null);
        merchantAnalyzeQuery.setCreateEnd(null);
        MerchantAnalyze history = merchantAnalyzeService.sum(merchantAnalyzeQuery);

        map.put("today", today);
        map.put("yesterday", yesterday);
        map.put("beforeYDay", beforeYDay);
        map.put("week", week);
        map.put("month", month);
        map.put("history", history);
        return map;
    }


    /**
     * 时间列表
     * @param length 长度
     * @return 时间
     */
    public List<Date> dateList(Integer length){
        length = null == length ? 7: length;
        Date now = new Date();
        Date startDate = null;
        if(length == 30){
            startDate = addMonth(DateUtils.getTodayDate(), -1);
        }else{
            startDate = DateUtils.addDay(now, -(length - 1));
        }
        List<Date> dateList =  DateUtils.getDateDifference(startDate, now);
        return dateList;
    }

    /**
     * 时间列表
     * @param length 长度
     * @return 时间
     */
    public List<String> dateStringList(Integer length){
        List<String> dateList = Lists.newArrayList();
        //按周计算
        if (0 == length){
            dateList = Lists.newArrayList("第1周", "第2周", "第3周", "第4周", "第5周",
                    "第6周", "第7周", "第8周", "第9周", "第10周", "第11周(上周)", "第12周(本周)");
        }
        //按月计算
        else if (-1 == length){
            for (int i= 11; i>=0; i--){
                String date = DateUtils.formatDate(DateUtils.getFirstDayOfMonth(-i), DateUtils.MONTH_PATTERN);
                dateList.add(date);
            }
        } else if (-2 == length){
            for (int i= 23; i>=0; i--){
                String date = DateUtils.formatDate(DateUtils.getFirstDayOfMonth(-i), DateUtils.MONTH_PATTERN);
                dateList.add(date);
            }
        } else {
            List<Date> list = dateList(length);
            dateList = Lists.newArrayList();
            for (Date date : list){
                dateList.add(DateUtils.formatDate(date));
            }
        }
        return dateList;
    }


    /**
     * 时间长度列表
     * @param length 长度
     * @param memberId 会员编号
     * @return 列表数据
     */
    public List<MerchantAnalyze> lengthList(Integer length, String memberId){
        String now = DateUtils.todayDate();
        String startDay = null;
        if(length == 30){
            startDay = DateUtils.formatDate(addMonth(DateUtils.getTodayDate(), -1));
        }else{
            startDay = DateUtils.formatDate(DateUtils.addDay(now, -(length - 1)));
        }
        MerchantAnalyzeQuery merchantAnalyzeQuery = new MerchantAnalyzeQuery();
        merchantAnalyzeQuery.setMemberId(memberId);
        merchantAnalyzeQuery.setCreateStart(startDay);
        merchantAnalyzeQuery.setCreateEnd(now);
        return merchantAnalyzeService.queryList(merchantAnalyzeQuery, null);
    }

    /**
     * 报名列表
     * @param length 长度
     * @param memberId 会员编号
     * @return 报名列表
     */
    public List<String> chartList(Integer length, String memberId, Integer type){
        List<String> applyList = Lists.newArrayList();
        List<MerchantAnalyze> list = this.lengthList(length, memberId);
        List<Date> dateList = this.dateList(length);
        for (Date date : dateList){
            Object num = 0;
            for (MerchantAnalyze merchantAnalyze : list){
                if (DateUtils.formatDate(date).equals(DateUtils.formatDate(merchantAnalyze.getCreateDate()))){
                    if (type == 0){
                        num = merchantAnalyze.getApplyNum();
                    }
                    else if (type == 1){
                        num = merchantAnalyze.getOrderNum();
                    }
                    else if (type == 2){
                        num = merchantAnalyze.getMoney();
                    }
                    else if (type == 3){
                        num = merchantAnalyze.getViewNum();
                    }
                    else if (type == 4){
                        num = merchantAnalyze.getMemberNum();
                    }
                    else {
                        num = merchantAnalyze.getApplyNum();
                    }
                    break;
                }
            }
            applyList.add(num.toString());
        }
        return applyList;
    }

    /**
     * 管理员视图数据
     *
     * @param length 长度
     * @param type 类型
     * @return 视图数据
     */
    public List<String> adminChartList(Integer length,Integer type){
        List<String> applyList = Lists.newArrayList();

        String now = DateUtils.todayDate();
        String startDay = null;
        if(length == 30){
            startDay = DateUtils.formatDate(addMonth(DateUtils.getTodayDate(), -1));
        }else{
            startDay = DateUtils.formatDate(DateUtils.addDay(now, -(length - 1)));
        }
        MerchantAnalyzeQuery merchantAnalyzeQuery = new MerchantAnalyzeQuery();
        merchantAnalyzeQuery.setCreateStart(startDay);
        merchantAnalyzeQuery.setCreateEnd(now);
        List<MerchantAnalyze> list = merchantAnalyzeService.adminChartList(merchantAnalyzeQuery);

        List<Date> dateList = this.dateList(length);
        for (Date date : dateList){
            Object num = 0;
            for (MerchantAnalyze merchantAnalyze : list){
                if (DateUtils.formatDate(date).equals(DateUtils.formatDate(merchantAnalyze.getCreateDate()))){
                    if (type == 0){
                        num = merchantAnalyze.getApplyNum();
                    }
                    else if (type == 1){
                        num = merchantAnalyze.getOrderNum();
                    }
                    else if (type == 2){
                        num = merchantAnalyze.getMoney();
                    }
                    else if (type == 3){
                        num = merchantAnalyze.getViewNum();
                    }
                    else if (type == 4){
                        num = merchantAnalyze.getMemberNum();
                    }
                    else {
                        num = merchantAnalyze.getApplyNum();
                    }
                    break;
                }
            }
            applyList.add(num.toString());
        }
        return applyList;
    }

    /**
     * admin统计详情
     *
     * @return 详情
     */
    public HashMap<String, MerchantAnalyze> adminDetail(){
        HashMap<String, MerchantAnalyze> map = Maps.newHashMap();

        String now = DateUtils.todayDate();
        MerchantAnalyzeQuery merchantAnalyzeQuery = new MerchantAnalyzeQuery();
        merchantAnalyzeQuery.setCreateStart(now);
        merchantAnalyzeQuery.setCreateEnd(now);

        //今日
        MerchantAnalyze today = merchantAnalyzeService.adminSum(merchantAnalyzeQuery);

        //昨日
        String day = DateUtils.formatDate(DateUtils.addDay(now, -1));
        merchantAnalyzeQuery.setCreateStart(day);
        merchantAnalyzeQuery.setCreateEnd(day);
        MerchantAnalyze yesterday = merchantAnalyzeService.adminSum(merchantAnalyzeQuery);

        //前日
        day = DateUtils.formatDate(DateUtils.addDay(now, -2));
        merchantAnalyzeQuery.setCreateStart(day);
        merchantAnalyzeQuery.setCreateEnd(day);
        MerchantAnalyze beforeYDay = merchantAnalyzeService.adminSum(merchantAnalyzeQuery);

        //本周
        merchantAnalyzeQuery.setCreateStart(DateUtils.formatDate(DateUtils.getFirstDayOfWeek(0)));
        merchantAnalyzeQuery.setCreateEnd(DateUtils.formatDate(DateUtils.getLastDayOfWeek(0)));
        MerchantAnalyze week = merchantAnalyzeService.adminSum(merchantAnalyzeQuery);

        //本月
        merchantAnalyzeQuery.setCreateStart(DateUtils.formatDate(DateUtils.getFirstDayOfMonth(0)));
        merchantAnalyzeQuery.setCreateEnd(DateUtils.formatDate(DateUtils.getLastDayOfMonth(0)));
        MerchantAnalyze month = merchantAnalyzeService.adminSum(merchantAnalyzeQuery);

        //历史
        merchantAnalyzeQuery.setCreateStart(null);
        merchantAnalyzeQuery.setCreateEnd(null);
        MerchantAnalyze history = merchantAnalyzeService.adminSum(merchantAnalyzeQuery);

        map.put("today", today);
        map.put("yesterday", yesterday);
        map.put("beforeYDay", beforeYDay);
        map.put("week", week);
        map.put("month", month);
        map.put("history", history);
        return map;
    }

}
